package l1j.server.server.model.damage;

import static l1j.server.server.model.skill.L1SkillId.FINAL_BURN;
import static l1j.server.server.model.skill.L1SkillId.ILLUSION_AVATAR;
import static l1j.server.server.model.skill.L1SkillId.JOY_OF_PAIN;
import l1j.server.server.datatables.EnchantWeaponAdvance;
import l1j.server.server.datatables.EnchantWeaponAdvance.WeaponEnchantAdvance;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.model.Instance.L1PcInstance;
import l1j.server.server.templates.L1Skills;
import l1j.server.server.utils.Random;

public class L1PcMagic {
	private L1PcInstance _attacker;

	private L1Dmg _dmg;

	public L1PcMagic(L1PcInstance attacker, L1Dmg dmg) {
		_attacker = attacker;
		_dmg = dmg;
	}

	public void calcPcDmg() {
		L1Skills skill = _attacker.getCurrentSkill();
		int skillId = skill.getId();

		// 疼痛的歡愉傷害：(最大血量 - 目前血量 /5)
		if (skillId == JOY_OF_PAIN) {
			_dmg.add(_attacker.getMaxHp() - _attacker.getCurrentHp());
			_dmg.div(5);
		} else {
			if (skillId == FINAL_BURN) {
				_dmg.add(_attacker.getCurrentMp());
			} else {
				_dmg.add(skill.calcMagicDiceDamage());
				_dmg.mul(_attacker.calcMagicCoef());
				// 出生魔法爆擊率
				if (skill.getSkillLevel() <= 6
						&& Random.chance100(_attacker
								.getOriginalMagicCritical() + 10)) {
					_dmg.mul(1.5);
				}

				_dmg.add(_attacker.getOriginalMagicDamage());
				if (_attacker.hasSkillEffect(ILLUSION_AVATAR)) {
					_dmg.add(10);
				}

			}
		}

		// 武器強化值額外獎勵處理
		L1ItemInstance weapon = _attacker.getWeapon();
		if (weapon != null) {
			WeaponEnchantAdvance wea = EnchantWeaponAdvance.getInstance()
					.getAdvance(weapon.getEnchantLevel());
			_dmg.add(wea.getExtra_magic_dmg());
			if (Random.chance100(wea.getCritical_magic_chance())) {
				_dmg.mul(1.0 + wea.getCritical_magic_rate() / 100.0);
			}
		}
	}
}
